<?php

namespace teia\webdev\php\core;

class ProductItemTrack {
	private $items;
	
	public function __construct() {
		$this->items = array();
	}
	
	public function push_prod_it($prod_id, $item_id, $visit_count=0, $cart_count=0) { 
		$prod_it_id = product_item_t::generate_id($prod_id, $item_id);
		if (isset($this->items[$prod_it_id])) {
			$this->items[$prod_it_id]->update($visit_count, $cart_count);
		} else {
			$this->items[$prod_it_id] = new product_item_track_t($prod_id, $item_id, $visit_count, $cart_count);
		}
	}
	
	public function pop_prod_it($prod_id, $item_id) {
		$prod_it_id = product_item_t::generate_id($prod_id, $item_id);
		if (isset($this->items[$prod_it_id])) {
			$prod_it = $this->items[$prod_it_id];
			unset($this->items[$prod_it_id]);
			return $prod_it;
		} else {
			return null;
		}
	}
	
	public function get_all_prod_its($min_visit=1, $min_cart=0) {
		$prod_its = array();
		foreach ($this->items as $id => $prod_it_track) {
			$visit_count = $prod_it_track->get('visit_count');
			$cart_count = $prod_it_track->get('cart_count');
			if ($visit_count >= $min_visit && $cart_count >= $min_cart) {
				$prod_its[$id] = $prod_it_track->get('prod_it');
			}
		}
		return $prod_its;
	}
	
	public function get_sorted_prod_its($min_visit=1, $min_cart=0, $max_visit=PHP_INT_MAX, $max_cart=PHP_INT_MAX) {
		$prod_its = array();
		$sort_cart = $this->sort('cart_count', $min_cart, $max_cart);
		$sort_visit =$this->sort('visit_count', $min_visit, $max_visit);
		$sort_all = array_merge($sort_cart, $sort_visit);
		$sort_all = array_unique($sort_all);
		foreach ($sort_all as $id => $prod_it_track) {
			$prod_its[$id] = $prod_it_track->get('prod_it');
		}
		return $prod_its;
	}
	
	public function get_all_prod_objs($min_visit=1, $min_cart=0) {
		$prod_it_objs = array();
		foreach ($this->items as $prod_it_track) {
			$visit_count = $prod_it_track->get('visit_count');
			$cart_count = $prod_it_track->get('cart_count');
			if ($visit_count >= $min_visit && $cart_count >= $min_cart) {
				$prod_it = $prod_it_track->get('prod_it');
				$prod_obj =  $prod_it->fetch_prod_obj();
				$prod_id = $prod_obj->get('prod_id');
				$prod_its[$prod_id] =$prod_it_objs;
			}
		}
		return $prod_its;
	}
	
	public function sort($property, $min=0, $max=PHP_INT_MAX, $ascend=FALSE) {
		$values = array();
		foreach ($this->items as $id => $prod_it_track) {
			$count = $prod_it_track->get($property);
			if ($count >= $min && $count <= $max) { 
				$values[$id] = $count;
			}
		}
		if ($ascend) {
			sort($values);
		} else {
			arsort($values);
		}
		return array_keys($values);
	}
	
	public function sort_all($min_visit=1, $min_cart=0, $max_visit=PHP_INT_MAX, $max_cart=PHP_INT_MAX) {
		$prod_its = array();
		$sort_cart = $this->sort('cart_count', $min_cart, $max_cart);
		$sort_visit =$this->sort('visit_count', $min_visit, $max_visit);
		$sort_all = array_merge($sort_cart, $sort_visit);
		
		return array_unique($sort_all);
	}
	
	public function generate_filter($min_visit=1, $min_cart=0, $limit=PHP_INT_MAX) {
		$filter = array('prod_filter' => array(), 'item_filter' => array());
		$prod_ids = array();
		$item_ids = array();
		
		$prod_its = $this->get_all_prod_its($min_visit, $min_cart);
		$prod_its = FunctionPool::array_limit($prod_its, $limit);
		foreach ($prod_its as $prod_it) {
			$prod_id = $prod_it->get('prod_id');
			$item_id = $prod_it->get('item_id');
			array_push($prod_ids, $prod_id);
			array_push($item_ids, $item_id);
		}
		$filter['prod_filter']['prod_id'] = FunctionPool::array_single($prod_ids);
		$filter['item_filter']['item_id'] = FunctionPool::array_single($item_ids);
		//print_r($filter);
		return $filter;
	}
}

class product_item_track_t {
	private $prod_it;
	private $cart_count;
	private $visit_count;

	public function __construct($prod_id, $item_id, $visit_count=0, $cart_count=0) {
		$this->prod_it = new product_item_t($prod_id, $item_id);
		$this->visit_count = $visit_count;
		$this->cart_count = $cart_count;
	}
	
	public function get($property) {
		return isset($this->$property) ? $this->$property : null;
	}
	
	public function get_id() {
		return product_item_t::generate_id($this->prod_id, $this->item_id);	
	}
	
	public function update($visit_count=0, $cart_count=0) {
		$this->visit_count += $visit_count;
		$this->cart_count += $cart_count;
	}
	
	public function update_visit_count($visit_count) {
		$this->visit_count += $visit_count;
	}
	
	public function update_cart_count($cart_count) {
		$this->cart_count += $cart_count;
	}
}

?>